package com.hnkywl.sts.action.base;

import com.hnkywl.sts.entity.base.Gspz;
import com.hnkywl.sts.entity.sys.Yg;
import com.hnkywl.sts.service.base.GspzService;
import com.hnkywl.sts.service.sys.YgService;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-5-20
 * Time: 下午8:51
 * To change this template use File | Settings | File Templates.
 */
@Namespace("/base/gspz")
public class GspzAction extends SimpleActionSupport {

    private GspzService gspzService;

    //名称
    private String mc;
    //分页组件 起始位置
    private int page;
    //分页组件 页面大小
    private int rows = 10;

    private Long gspzId;

    private Long xshtId;

    private Gspz gspz;

    private Yg curYg = YgService.getCurrentYg();

    private String validateVal;
    //需要远程ajax验证的属性
    private String validateField;

    @Action("gspzList")
    public void gspzList() {
        this.renderJson(this.gspzService.findGspzList(mc, page, rows));
    }

    //销售合同查询公式时，只显示状态为已启用的公式
    @Action("gspzList01")
    public void gspzList01() {
        this.renderJson(this.gspzService.findGspzList01(mc, page, rows));
    }

    @Action("gspzListByHtId")
    public void gspzListByHtId() {
        this.renderJson(this.gspzService.findGspzListByHtId(xshtId, 1, 100));
    }

    @Action("adsqList")
    public void adsqList() {
        this.renderJson(this.gspzService.findAdsqList(gspzId, 1, 100));
    }

    @Action("atsqList")
    public void atsqList() {
        this.renderJson(this.gspzService.findAtsqList(gspzId, 1, 100));
    }

    @Actions({@Action("gspz_content"), @Action("gspz_detail")})
    public String gspzContent() {
        if (gspzId != null) {
            gspz = gspzService.findGspzById(gspzId);
        }
        return SUCCESS;
    }

    @Action("add_gspz")
    public void addGspz() {
        try {
            this.gspzService.addGspz(gspz);
            renderText("{\"statusCode\":\"200\", \"message\":\"新增成功！\", \"operationType\":\"add\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    @Action("update_gspz")
    public void update_gspz() {
        try {
            this.gspzService.addGspz(gspz);
            renderText("{\"statusCode\":\"200\", \"message\":\"修改成功！\", \"operationType\":\"add\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderText("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    @Action("start_gspz")
    public void start_gspz() {
        try {
            this.gspzService.startGspz(gspzId);
            renderJson("{\"statusCode\":\"200\", \"message\":\"启用成功！\", \"operationType\":\"add\"}");
        } catch (RuntimeException e) {
            e.printStackTrace();
            renderJson("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    @Action("stop_gspz")
    public void stop_gspz() {
        try {
            this.gspzService.stopGspz(gspzId);
            renderJson("{\"statusCode\":\"200\", \"message\":\"停用成功！\", \"operationType\":\"add\"}");
        } catch (Exception e) {
            e.printStackTrace();
            renderJson("{\"statusCode\":\"300\", \"message\":\"" + e.getMessage() + "\"}");
        }
    }

    @Action("ajax_validate")
    public void ajaxValidate() {
        if (!gspzService.ajaxValidate(gspzId, validateField, validateVal))
            renderJson("{\"valid\":true}");
        else
            renderJson("{\"valid\":false}");
    }

    public String getValidateVal() {
        return validateVal;
    }

    public void setValidateVal(String validateVal) {
        this.validateVal = validateVal;
    }

    public String getValidateField() {
        return validateField;
    }

    public void setValidateField(String validateField) {
        this.validateField = validateField;
    }

    public Long getXshtId() {
        return xshtId;
    }

    public void setXshtId(Long xshtId) {
        this.xshtId = xshtId;
    }

    public Long getGspzId() {
        return gspzId;
    }

    public void setGspzId(Long gspzId) {
        this.gspzId = gspzId;
    }

    public Gspz getGspz() {
        return gspz;
    }

    public void setGspz(Gspz gspz) {
        this.gspz = gspz;
    }

    public Yg getCurYg() {
        return curYg;
    }

    public void setCurYg(Yg curYg) {
        this.curYg = curYg;
    }

    public String getMc() {
        return mc;
    }

    public void setMc(String mc) {
        this.mc = mc;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    @Autowired
    public void setGspzService(GspzService gspzService) {
        this.gspzService = gspzService;
    }
}
